package com.ethink.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.enums.SaleReturnOrderReturnStatusEnum;
import com.ethink.order.api.model.SaleReturnDetailModel;
import com.ethink.order.api.model.SaleReturnModeModel;
import com.ethink.order.api.model.SaleReturnModel;
import com.ethink.order.api.request.*;
import com.ethink.order.api.response.SaleReturnOrderAddResponse;
import com.ethink.order.api.response.SaleReturnQueryDetailResponse;
import com.ethink.order.common.entity.SaleReturnDetailOrder;
import com.ethink.order.common.entity.SaleReturnOrder;
import com.ethink.order.common.entity.TobSaleReturnOrder;
import com.ethink.order.common.mapper.SaleReturnDetailOrderMapper;
import com.ethink.order.common.mapper.SaleReturnOrderMapper;
import com.ethink.order.service.SaleReturnOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 销售退单 Service接口实现
 *
 * @author default
 * @date 2022-12-09
 */
@Slf4j
@Service
public class SaleReturnOrderServiceImpl implements SaleReturnOrderService {

    @Autowired
    private SaleReturnOrderMapper saleReturnOrderMapper;

    @Autowired
    private SaleReturnDetailOrderMapper saleReturnDetailOrderMapper;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public SaleReturnOrderAddResponse add(SaleReturnOrderAddRequest request) {
        SaleReturnOrderAddResponse addResponse = new SaleReturnOrderAddResponse();
        String orderNo = idGenerator.xstd();
        SaleReturnOrder saleReturnOrder = BeanUtil.copyProperties(request, SaleReturnOrder.class, "item");
        saleReturnOrder.setOrderNo(orderNo);
        saleReturnOrderMapper.insert(saleReturnOrder);
        for (SaleReturnDetailOrderAddRequest detailOrderAddRequest : request.getItem()) {
            String detailOrderNo = idGenerator.xstdxd();
            SaleReturnDetailOrder saleReturnDetailOrder = BeanUtil.copyProperties(detailOrderAddRequest, SaleReturnDetailOrder.class);
            saleReturnDetailOrder.setDetailOrderNo(detailOrderNo);
            saleReturnDetailOrder.setOrderNo(orderNo);
            saleReturnDetailOrderMapper.insert(saleReturnDetailOrder);
        }
        addResponse.setOrderNo(orderNo);
        return addResponse;
    }

    @Override
    public Page<SaleReturnModel> pageQuery(SaleReturnOrderQueryRequest request) {
        return saleReturnOrderMapper.pageQuery(PageUtils.buildPage(request), request);
    }

    @Override
    public SaleReturnQueryDetailResponse queryDetail(Long id) {
        SaleReturnQueryDetailResponse response = new SaleReturnQueryDetailResponse();
        SaleReturnOrder saleReturnOrder = saleReturnOrderMapper.selectById(id);
        if (ObjectUtil.isEmpty(saleReturnOrder)) {
            return response;
        }
        SaleReturnModel saleReturnModel = BeanUtil.copyProperties(saleReturnOrder, SaleReturnModel.class);
        List<SaleReturnDetailModel> item = saleReturnDetailOrderMapper.selectDetailByOrderNo(saleReturnOrder.getOrderNo());
        response.setItem(item);
        int sum = item.stream().mapToInt(SaleReturnDetailModel::getReturnQty).sum();
        saleReturnModel.setTotalReturnQty(sum);
        response.setMain(saleReturnModel);
        return response;
    }

    @Override
    public Boolean update(SaleReturnOrderUpdateRequest request) {
        SaleReturnOrder saleReturnOrder = saleReturnOrderMapper.selectById(request.getId());
        //先删在增
        saleReturnDetailOrderMapper.delete(new LambdaQueryWrapper<SaleReturnDetailOrder>()
                .eq(SaleReturnDetailOrder::getOrderNo, saleReturnOrder.getOrderNo()));
        for (SaleReturnDetailOrderAddRequest detailOrderAddRequest : request.getItem()) {
            SaleReturnDetailOrder saleReturnDetailOrder = BeanUtil.copyProperties(detailOrderAddRequest, SaleReturnDetailOrder.class);
            saleReturnDetailOrder.setOrderNo(saleReturnOrder.getOrderNo());
            saleReturnDetailOrder.setDetailOrderNo(idGenerator.xstdxd());
            saleReturnDetailOrderMapper.insert(saleReturnDetailOrder);
        }
        BeanUtil.copyProperties(request, saleReturnOrder, "item");
        return saleReturnOrderMapper.updateById(saleReturnOrder) > 0;
    }

    @Override
    public Boolean delete(Long id) {
        SaleReturnOrder saleReturnOrder = saleReturnOrderMapper.selectById(id);
        if (!SaleReturnOrderReturnStatusEnum.S1.getIntValue().equals(saleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "仅支持草稿状态删除");
        }
        return saleReturnOrderMapper.delById(id) > 0;
    }

    @Override
    public Boolean pass(Long id) {
        SaleReturnOrder saleReturnOrder = saleReturnOrderMapper.selectById(id);
        if (!SaleReturnOrderReturnStatusEnum.S2.getIntValue().equals(saleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }
        UpdateStatusBaseRequest request = new UpdateStatusBaseRequest();
        request.setId(id);
        request.setOrderStatus(SaleReturnOrderReturnStatusEnum.S3.getIntValue());
        return updateStatus(request);
    }

    @Override
    public Boolean refuse(Long id) {
        SaleReturnOrder saleReturnOrder = saleReturnOrderMapper.selectById(id);
        if (!SaleReturnOrderReturnStatusEnum.S2.getIntValue().equals(saleReturnOrder.getReturnStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }
        UpdateStatusBaseRequest request = new UpdateStatusBaseRequest();
        request.setId(id);
        request.setOrderStatus(SaleReturnOrderReturnStatusEnum.S1.getIntValue());
        return updateStatus(request);
    }

    @Override
    public Boolean updateStatus(UpdateStatusBaseRequest request) {
        return saleReturnOrderMapper.update(null, new LambdaUpdateWrapper<SaleReturnOrder>()
                .eq(SaleReturnOrder::getId, request.getId())
                .set(SaleReturnOrder::getReturnStatus, request.getOrderStatus())) > 0;
    }

    @Override
    public Boolean saleReturnOrderStatusFinish(List<String> orderNo) {
        if (CollUtil.isEmpty(orderNo)) {
            return true;
        }
        return saleReturnOrderMapper.update(null, new LambdaUpdateWrapper<SaleReturnOrder>()
                .in(SaleReturnOrder::getOrderNo, orderNo)
                .set(SaleReturnOrder::getReturnStatus, SaleReturnOrderReturnStatusEnum.S4.getIntValue())) > 0;
    }

    @Override
    public Page<SaleReturnModeModel> pageQueryMode(SaleReturnOrderQueryModeRequest request) {
        return saleReturnOrderMapper.pageQueryMode(PageUtils.buildPage(request), request);
    }
}
